package com.app.controller.supermarket;


import com.app.common.EducationTool;
import com.app.model.supermarket.MktCertModel;
import com.app.model.supermarket.MktPersonModel;
import com.app.service.supermarket.*;
import com.app.util.*;
import com.app.vo.supermarket.MktLoginVo;
import com.app.vo.supermarket.MktPersonReturnVo;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

@Controller
@RestController
public class MktPersonController {

    @Autowired
    private MktPersonService mktPersonService;

    @Autowired
    private MktCertService mktCertService;

    @Autowired
    private MyCollectService myCollectService;
    @Autowired
    OddJobReleaseService oddJobReleaseService;
    @Autowired
    MyOddService myOddService;



//增/改
    @GetMapping("mktCurd")
   public Result<Object> insert(
                        @RequestParam("faceUrl") String faceUrl,
                        @RequestParam("name") String name,
                        @RequestParam("birthday") String birthday,
                        @RequestParam("location") String location,
                        @RequestParam("education") String education,
                        @RequestParam("experience") String experience,
                        @RequestParam("certificateUrls") String certificateUrls,
                        @RequestParam("sex") Integer sex,
                        HttpServletRequest request
                       ) throws ParseException {
        String token = request.getHeader("token");
        if(token==null)
        {
            return Result.error(405,"token不存在");
        }
        String mobile = TokenUtils.verify(token);
        MktPersonModel mktPerson = mktPersonService.getModelByMobile(mobile);
        if(mktPerson==null)
        {
            return Result.error(405,"用户不存在");
        }
        mktPerson.setFaceUrl(faceUrl);
        mktPerson.setName(name);
        mktPerson.setBirthday(birthday);
        mktPerson.setLocation(location);
        mktPerson.setEducation(education);
        mktPerson.setExperience(experience);
        mktPerson.setSex(sex);
        mktCertService.delete(mktPerson.getId());
        String str[]=certificateUrls.split(",");
        for(int i=0;i<str.length;i++){
            MktCertModel model = new MktCertModel();
            model.setMid(mktPerson.getId());
            model.setUrl(str[i]);
            mktCertService.insert(model);
        }
        mktPersonService.update(mktPerson);
        return Result.success("操作成功");
   }

    @PostMapping("doUpLicense")
    public Result<Object> doUpLicense(
            @RequestParam("licenseUrl") String licenseUrl,
            @RequestParam("comName") String comName,
            HttpServletRequest request
    ) throws ParseException {
        String token = request.getHeader("token");
        if(token==null)
        {
            return Result.error(405,"token不存在");
        }
        String mobile = TokenUtils.verify(token);
        MktPersonModel mktPerson = mktPersonService.getModelByMobile(mobile);
        if(mktPerson==null)
        {
            return Result.error(405,"用户不存在");
        }
        mktPerson.setLicenseUrl(licenseUrl);
        mktPerson.setComName(comName);
        mktPersonService.update(mktPerson);

        return Result.success("操作成功");
    }

   @GetMapping("getPersonInf")
    public Result<Object> getPersonInf(HttpServletRequest  request) throws Exception{
       String token = request.getHeader("token");
       if(token==null)
       {
           return Result.error(405,"token不存在");
       }
       String mobile = TokenUtils.verify(token);
       MktPersonModel customer = mktPersonService.getModelByMobile(mobile);
       if(customer==null)
       {
           return Result.error(405,"用户不存在");
       }
       if (customer.getBirthday() != null)//年龄
       {
           Timestamp time = new Timestamp(new SimpleDateFormat("yyyy-MM-dd").parse(customer.getBirthday() + "-01").getTime());
           String date = DateUtil.yearCompare(time, CommonHelp.getNowTimestamp());
           customer.setAge(date);
       }
       //学历
       String eduName = "";
       //学历条件筛选
       if (customer.getEducation() != null ) {
           int toUp = 0;
           for (int k = 0; k < EducationTool.EDUCATION_VO_LIST.size(); k++) {
               if (Integer.parseInt(customer.getEducation()) == EducationTool.EDUCATION_VO_LIST.get(k).getId()) {
                   eduName = EducationTool.EDUCATION_VO_LIST.get(k).getData();
               }
           }
           customer.setEducation(eduName);
       } else {
           customer.setEducation("暂无学历");
       }
       int countPush = oddJobReleaseService.countMid(customer.getId());
       int  countCollect = myCollectService.countMid(customer.getId());
       int countOdd = myOddService.countMid(customer.getId());
       MktPersonReturnVo vo = new MktPersonReturnVo();
       vo.setCustomer(customer);
       vo.setCountOdd(countOdd);
       vo.setCountCollect(countCollect);
       vo.setCountPush(countPush);
       return Result.success(vo);
   }

}
